import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wallet_utils/event_bus_utils.dart';
import 'package:wallet_utils/msg_event_utils.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:wallet_utils/el_refresh.dart';
import 'package:wallet_utils/report_widget/base/callback/base_callback.dart';
import 'package:wallet_utils/toast.dart';
import 'package:wallet_utils/webview_common.dart';
import '';
import '../../agreement_dialog.dart';
import '../../home/api/hxh_api.dart';
import '../../home/bean/hxhConfirmLoanBean.dart';
import '../../home/bean/hxhOrderBean.dart';
import '../bean/repaymentBean.dart';
import 'state.dart';

class RepaymentLogic extends GetxController {
  final RepaymentState state = RepaymentState();
  List<RepaymentList>? repaymentList=[];
  List<OrderList> orderList=[];

  @override
  void onInit()  {
    super.onInit();
    EventBusUtils.getInstance().on<MsgEventBusUtils>().listen((event) {
      //是否显示产品状态
      if (event.type == MsgEventBusUtils.productStatus) {
        state.refreshController = RefreshController(initialRefresh: false);
        getUserOrder();
        }
    });
    getUserOrder();
  }
  ///订单列表
  getUserOrder() {
    HXHApi.getUserAllOrder(BaseCallback(onSuccess: (map) {
      HxhOrderBean orderBean = HxhOrderBean.fromJson(map);
      if (orderBean.data != null) {
        orderList = orderBean.data!;
        if(!orderList.isEmpty){
          OrderList model = orderList[orderList.length-1];
          if(model.auditStatus==131){
            state.interest = (model.orderYearRate!*100).toString();
            getRepayPlan(model.orderNo!);
          }
        }
      } else {
        ToastUtils.showToast(orderBean.msg ?? "");
      }
      update();
      refreshCompelet(state.refreshController);
    }, onError: (msg) {
      Future.delayed(const Duration(milliseconds: 500), () {
        ToastUtils.showToast(msg ?? "");
        update();
        refreshCompelet(state.refreshController);
      });
    }));
  }
  //获取还款计划列表
  getRepayPlan(String orderNo) async{
    HXHApi.getRepayPlan(
        orderNo,
        BaseCallback(onSuccess: (map) {
          RepaymentBean repaymentBean = RepaymentBean.fromJson(map);
          if (repaymentBean.data != null) {
            repaymentList = repaymentBean.data.plans;
            state.model = repaymentList![0];
            state.orderNo = repaymentBean.data.orderNo;
          } else {
            // ToastUtils.showToast(repaymentBean.msg ?? "");
          }
          update();
          refreshCompelet(state.refreshController);
        }, onError: (msg) {
          Future.delayed(const Duration(milliseconds: 500), () {
            refreshCompelet(state.refreshController);
            ToastUtils.showToast(msg ?? "");
            update();
          });
        }));
  }
  //获取还款页面接口
  getRepayUrl(String orderNo) async{
    HXHApi.getRepayUrl(
        orderNo,
        BaseCallback(onSuccess: (map) {
          HxhConfirmLoanBean hxhConfirmLoanBean =
          HxhConfirmLoanBean.fromJson(map);
          state.url = hxhConfirmLoanBean.result.url;
          Get.to(WebViewCommon(
            state.url,
          ))?.then((value) {
            getRepayPlan(orderNo);
          });
        }, onError: (msg) {
          Future.delayed(const Duration(milliseconds: 500), () {
            ToastUtils.showToast(msg ?? "");
            update();
          });
        }));
  }

  //获取角标图片 801 待还款 802 已还款 803 还款中 804 已逾期
  getTagImge(int status){
    if(status==801){
      return 'images/re_tag_daihuankuan.png';
    }else if(status==802){
      return 'images/re_tag_yihuankuan.png';
    }else if(status==803){
      return 'images/re_tag_huankuanzhong.png';
    }else if(status==804){
      return 'images/re_tag_yiyuqi.png';
    }else{
      return '';
    }
  }
  //获取全部待还
  getTotalRepayAmount(){
    int totalRepayAmount = 0;
    for(int i = 0;i < repaymentList!.length;i++){
      RepaymentList model = repaymentList![i];
      if(model.status==801 || model.status==804){
        totalRepayAmount+=model.totalRepayAmount;
      }
    }
    return (totalRepayAmount/100).toString();
  }
  //获取总服务费
  getTotalServiceAmount(){
    int totalServiceAmount = 0;
    for(int i = 0;i < repaymentList!.length;i++){
      RepaymentList model = repaymentList![i];
      if(model.status==801 || model.status==804){
        totalServiceAmount+=model.repayPeriodServiceAmount;
      }
    }
    return (totalServiceAmount/100).toString();
  }
  //获取总利息
  getTotalInterestAmount(){
    int totalInterestAmount = 0;
    for(int i = 0;i < repaymentList!.length;i++){
      RepaymentList model = repaymentList![i];
      if(model.status==801 || model.status==804){
        totalInterestAmount+=model.repayInterestAmount;
      }
    }
    return (totalInterestAmount/100).toString();
  }

  //获取功能按钮文字
  getButtonStatus(int status){
    if(status==801){
      return true;
    }else if(status==802){
      return false;
    }else if(status==803){
      return false;
    }else if(status==804){
      return true;
    }else{
      return false;
    }
  }
}
